package org.darchitect.nn.som.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.darchitect.ui.UIUtil;

public class QuerySOMDialog extends JDialog {

	private static final long serialVersionUID = -3327993097450599298L;
	
	private JLabel[] labels;
	private JTextField[] textfields;
	
	public QuerySOMDialog(int vectorSize, ActionListener queryListener) {
		labels = new JLabel[vectorSize];
		textfields = new JTextField[vectorSize];
		
		JPanel comps = new JPanel();
		comps.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(3, 4, 2, 4);
		
		FocusListener focus = new FocusListener() {
			public void focusGained(FocusEvent e) {
				JTextField t = (JTextField) e.getSource();
				t.selectAll();
			}
			public void focusLost(FocusEvent e) {
			}
		};
		
		for (int i = 0;  i < vectorSize; i++) {
			labels[i] = new JLabel("Element #" + i);
			textfields[i] = new JTextField(10);
			textfields[i].setText("?");
			textfields[i].addFocusListener(focus);
			gbc.gridy = i;
			gbc.gridx = 0;
			comps.add(labels[i], gbc);
			gbc.gridx = 1;
			comps.add(textfields[i], gbc);
		}
		
		JButton queryBtn = new JButton("Query");
		JButton closeBtn = new JButton("Close");
		queryBtn.addActionListener(queryListener);
		closeBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		Box buttonBox = Box.createHorizontalBox();
		buttonBox.add(queryBtn);
		buttonBox.add(Box.createHorizontalStrut(10));
		buttonBox.add(closeBtn);
		
		Box mainBox = Box.createVerticalBox();
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(comps);
		mainBox.add(Box.createVerticalStrut(7));
		mainBox.add(buttonBox);
		mainBox.add(Box.createVerticalStrut(10));
		
		add(mainBox);
		pack();
		UIUtil.centerWindow(this);
		setTitle("Query SOM");
	}

	public double[] getQueryVector() {
		if (! valid())
			return null;
		double[] q = new double[textfields.length];
		String str;
		for (int i = 0; i < q.length; i++) {
			str = textfields[i].getText().trim();
			q[i] = str.equals("?") ? Double.MAX_VALUE : Double.parseDouble(str);
		}
		return q;
	}

	private boolean valid() {
		boolean valid = true;
		String str;
		for (int i = 0; i < textfields.length; i++) {
			str = textfields[i].getText().trim();
			if (! str.equals("?"))
				try {
					Double.parseDouble(str);
				} catch (NumberFormatException e) {
					valid = false;
				}
		}
		return valid;
	}

	public void setQueryAnswer(double[] ans) {
		for (int i = 0; i < ans.length; i++) {
			textfields[i].setText(Double.toString(ans[i]));
			textfields[i].setCaretPosition(0);
		}
	}

}
